package team.se.tms.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.se.tms.dao.Dao;
import team.se.tms.dto.GroupDetail;
import team.se.tms.exception.UpdateException;
import team.se.tms.factory.ModelFactory;
import team.se.tms.po.GroupInfo;
import team.se.tms.po.ProjectInfo;
import team.se.tms.po.StudentInfo;
import team.se.tms.service.GroupInfoService;
import team.se.tms.util.KeyUtil;

import java.util.*;

/**
 * Created by argentum on 2018/12/25.
 * @author argentum
 */
@Slf4j
@Service("groupInfoService")
public class GroupInfoServiceImpl implements GroupInfoService {
    @Autowired
    Dao dao;

    /**
     * 新建小组
     *
     * @create on 2018/12/28 by argentum
     *
     * @param teachCourseId
     * @param teacherId
     * @param projectName
     * @param stuIds
     * @param groupLeader
     * @return
     * @throws Exception
     */
    @Override
    public Boolean saveGroupInfo(String teachCourseId, String teacherId, String projectName,List<String> stuIds,String groupLeader) throws Exception{

        String projectId = KeyUtil.generatePrimaryKey();
        Date date = new Date();
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setProjectId(projectId);
        projectInfo.setProjectName(projectName);
        projectInfo.setProjectCreateTime(date);
        projectInfo.setProjectUpdateTime(date);
        projectInfo.setTeaId(teacherId);
        projectInfo.setTeachCourseId(teachCourseId);
        int count = dao.insert("ProjectInfoMapper.insertProjectInfo",projectInfo);
        List<GroupInfo> groupInfos = ModelFactory.createGroup(stuIds,projectId,groupLeader);
        int count2 = dao.insert("GroupInfoMapper.insertGroupInfo",groupInfos);
        if(count>0&&count2>0){
            return true;
        }
        return false;
    }

    /**
     * 编辑小组信息
     * @create on 2018/12/27 by argentum
     * @update on 2018/12/31 by argentum
     *
     * @param projectId
     * @param projectName
     * @param students
     * @param groupLeader
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyGroupInfo(String projectId, String projectName, List<String> students, String groupLeader) throws Exception {

        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setProjectId(projectId);
        projectInfo.setProjectName(projectName);
        projectInfo.setProjectUpdateTime(new Date());

        List<GroupInfo> groupInfos = ModelFactory.createGroup(students,projectId,groupLeader);

        int count = dao.update("ProjectInfoMapper.updateProjectInfoByProjectId",projectInfo);
        int count2 = 0;
        if(dao.delete("GroupInfoMapper.deleteGroupInfoByProjectId",projectId)>0){
           count2 = dao.insert("insertGroupInfo",groupInfos);
        }

        if(count<=0 || count2<=0){
            throw new UpdateException();
        }
        return true;
    }


    /**
     * 课程评分-查看所有小组成员及其成绩
     *
     * @param teachCourseId
     * @return
     * @throws Exception
     */
    @Override
    public List<GroupDetail> listGroupDetails(String teachCourseId) throws Exception {
        List<GroupDetail> groupDetails = dao.selectList("GroupInfoMapper.selectGroupDetailsByTeachCourseId",teachCourseId);
        log.debug("================{}");
        return groupDetails;
    }

}
